﻿using Adventure.Server.Library;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Adventure.Tests
{
    /// <summary>
    /// Tests the ObjectAttributes class.
    /// </summary>
    [TestClass]
    public class ObjectAttributesTest
    {
        /// <summary>
        /// Tests the methods to access a ObjectAttributes instance.
        /// </summary>
        [TestMethod]
        public void ObjectAttributesAccessTests()
        {
            var attributes = new ObjectAttributes();

            // Test the reading of non existent keys
            Assert.IsNull(attributes["Test"]);
            Assert.IsNull(attributes.GetAttributeAsString("Test"));
            Assert.IsNull(attributes.GetAttributeAsInt("Test"));

            // Add an attribute and read it.
            attributes["Test"] = "1";
            Assert.AreEqual("1", attributes["Test"]);
            Assert.AreEqual("1", attributes.GetAttributeAsString("Test"));
            Assert.AreEqual(1, attributes.GetAttributeAsInt("Test"));

            // Attribute that is not an int
            attributes["Test"] = "abc";
            Assert.IsNull(attributes.GetAttributeAsInt("Test"));
        }

        /// <summary>
        /// Tests the creation, comparison and Hashcode methods of ObjectAttributes
        /// </summary>
        [TestMethod]
        public void ObjectAttributesCreationAndComparisonTest()
        {
            var attributes1 = new ObjectAttributes();
            var attributes2 = new ObjectAttributes();
            Assert.IsTrue(attributes1.Equals(attributes2));
            Assert.AreEqual(attributes1.GetHashCode(), attributes2.GetHashCode());

            attributes1["test"] = "TestValue";
            Assert.IsFalse(attributes1.Equals(attributes2));
            Assert.AreNotEqual(attributes1.GetHashCode(), attributes2.GetHashCode());

            attributes2["test"] = "OtherTestValue";
            Assert.IsFalse(attributes1.Equals(attributes2));
            Assert.AreNotEqual(attributes1.GetHashCode(), attributes2.GetHashCode());

            attributes2["test"] = "TestValue";
            Assert.IsTrue(attributes1.Equals(attributes2));
            Assert.AreEqual(attributes1.GetHashCode(), attributes2.GetHashCode());
        }
    }
}
